package view.content;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.logging.Level;

import javax.imageio.ImageIO;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import model.contents.Content;
import net.HttpRequestFacade;
import util.CMYKtowebtool;
import util.LoggingUtils;

public class RecordSquareView extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public RecordSquareView(String topText, String bottomText, Content imageContent, int imageWidth, int imageHeight) {
		super();
		this.setLayout(new BorderLayout());
		JLabel topLabel = new JLabel(topText);
		topLabel.setPreferredSize(new Dimension(imageWidth, 30));
		topLabel.setHorizontalAlignment(SwingConstants.CENTER);
		JLabel bottomLabel = new JLabel(bottomText);
		bottomLabel.setHorizontalAlignment(SwingConstants.CENTER);
		bottomLabel.setPreferredSize(new Dimension(imageWidth, 30));
		this.add(topLabel, BorderLayout.NORTH);
		imageHeight -= 60;
		if (imageWidth > 0 && imageHeight > 0) {

			ContentBox cb = ContentBox.createContentBox(imageContent, imageWidth, imageHeight);

			if (cb != null) {
				this.add(cb, BorderLayout.CENTER);
			}
		} else {
			JPanel placeHolder = new JPanel();
			this.add(placeHolder, BorderLayout.CENTER);
			//System.out.println("invalid content dimensions, please adjust table height on webtool "+imageContent.getLocalPath());
		}
		this.add(bottomLabel, BorderLayout.SOUTH);
		this.setPreferredSize(new Dimension(imageWidth, imageHeight));
		this.setMinimumSize(new Dimension(imageWidth, imageHeight));
		// this.setBackground(PlayerUtilities.getRandomColor());
	}

	@SuppressWarnings("unused")
	private BufferedImage getImage(final Content c, int width, int height) {
		BufferedImage bi = null;
		try {
			bi = ImageIO.read(new File(c.getLocalPath()));
			bi = (BufferedImage) bi.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH);
		} catch (Exception e) {
			e.printStackTrace();

			Thread notificationThread = new Thread(new Runnable() {

				@Override
				public void run() {
					HttpRequestFacade.PostToConvertImage(c.getId());
				}
			});
			notificationThread.setName("Invalid Image Format Notification Thread");
			notificationThread.start();

			CMYKtowebtool.getInstance().setValue(c.getPath(), String.valueOf(c.getId()));


			LoggingUtils.log(Level.SEVERE,"Invalid image format notification getImage method"+e.getMessage(),e);
			c.delete();
		}
		return bi;
	}
}
